package view
{
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import models.ModelLocator;
	
	import utils.DrawUtils;
	
	public class SizeShow extends BaseFrame
	{
		
		private static var instance:SizeShow; 
		
		private var pic:Sprite;
		private var ui:Sprite;
		private var maxH:Number = 268;
		private var maxW:Number = 404;
		
		private var bitmap:Bitmap;
		private var basew:Number;
		private var baseh:Number;
		private var rate:Number = 6;
		private var ary:Array = ModelLocator.getInstance().sizeAry;
		public function SizeShow()
		{
			if(instance!=null) throw new Error("Error: Singletons can only be instantiated via getInstance() method!");  
			SizeShow.instance = this;  
			init();
		}
		public static function getInstance():SizeShow  
		{  
			if(instance==null)  instance = new SizeShow();  
			return instance; 
		}
		private function init():void{
			bitmap = ModelLocator.getInstance().bitmap;
			basew = bitmap.width;
			baseh = bitmap.height;
			ui = new Sprite;
			this.addChild(ui);
			pic = new Sprite;
			this.addChild(pic);
			pic.x = ui.x = 30;
			pic.y = ui.y = 55;
			//drawImg();
			setInfo("<font color='#6d5939' size='16' face='黑体' ><b>比例参考</b></font><font color='#666666'>   说明：本项规格为画芯尺寸，不含边框尺寸。 选择尺寸、画框、风格、调整构图后，再点击下一步。</font>");
		}
		public function drawRec(ary:Array):void{
			this.ary = ary;
			drawImg();
		}
		private function drawImg(begin:int=0):void{
			var g:Graphics = ui.graphics;
			g.clear();
			g.lineStyle(1,0xb5715d);
			var obj:Object = ary[ary.length-1];
			
			var sw:Number = obj.w/maxW;
			var sh:Number = obj.h/maxH;
			if(sw < sh){
				rate = maxH/obj.h;
			}else{
				rate = maxW/obj.w;
			}
			
			var p1:Point = new Point(0,0);
			var p2:Point = new Point(0,maxH);
			var p3:Point = new Point(maxW,maxH);
			var p4:Point = new Point(maxW,0);
			DrawUtils.drawDashed(g,p1,p2,5,5);
			DrawUtils.drawDashed(g,p2,p3,5,5);
			DrawUtils.drawDashed(g,p3,p4,5,5);
			DrawUtils.drawDashed(g,p4,p1,5,5);
			
			
			for(var i:int=begin;i<ary.length;i++){
				drawTwoLine(ary[i]);
			}
			
			
		}
		private function drawTwoLine(obj:Object):void{
			var w:Number = obj.w;
			var h:Number = obj.h;
			var g:Graphics = ui.graphics;
			var p1:Point = new Point(0,maxH - h*rate);
			var p2:Point = new Point(w*rate,maxH - h*rate);
			var p3:Point = new Point(w*rate,maxH);
			obj.p = p1;
			DrawUtils.drawDashed(g,p1,p2,5,5);
			DrawUtils.drawDashed(g,p2,p3,5,5);
			
			/*var txt:TextField = new TextField();
			txt.x = 30;
			txt.y = 18;
			this.addChild(txt);
			txt.width = 700;
			txt.height = 25;
			txt.selectable = false;
			txt.htmlText = "<font color='#6d5939' size='16' face='黑体' ><b>比例参考</b></font><font color='#666666'>   说明：本项规格为画芯尺寸，不含边框尺寸。 选择尺寸、画框、风格、调整构图后，再点击下一步。</font>";*/
		}
		public function clear():void{
			if(bitmap.parent){
				pic.removeChild(bitmap);
			}
			pic.graphics.clear();
		}
		public function onClick(w:Number,h:Number):void{
			if(bitmap.parent == null){
				pic.addChild(bitmap);
			}
			var g:Graphics = pic.graphics;
			var obj:Object = new Object;
			obj.x = 0;
			obj.y = maxH - h*rate;
			obj.w = w;
			obj.h = h;
			g.clear();
			g.beginFill(0xbbb5ac,0.5);
			g.drawRect(obj.x,obj.y,obj.w*rate+1,obj.h*rate+1);
			g.endFill();
			
			bitmap.x = obj.x;
			bitmap.y = obj.y;
			bitmap.visible = true;
			
			var w:Number = obj.w*rate+1;
			var h:Number = obj.h*rate+1;
			
			var sw:Number = w/basew;
			var sh:Number = h/baseh;
			if(sw < sh){
				bitmap.scaleX = bitmap.scaleY = sw;
				bitmap.y += (h - bitmap.height)/2;
			}else{
				bitmap.scaleX = bitmap.scaleY = sh;
				bitmap.x += (w-bitmap.width)/2;
			}
			for(var i:int=0;i<ary.length;i++){
				if(ary[i].w == obj.w && ary[i].h == obj.h){
					drawImg(i);
					break;
				}
			}
			
		}
	}
}